﻿using LightCAD.Core;
using LightCAD.Core.Elements;
using LightCAD.Runtime.Editor;
using SkiaSharp;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LightCAD.Runtime.Creator
{
    public class LineCreator : ElementCreator
    {
        public static string CmdName;
        public static CreateMethod[] Methods;
        public static CreateMethod DefaultMethod;
        static LineCreator()
        {
            Methods = new CreateMethod[1];
            CmdName = "LINE";
            var method = new CreateMethod();
            method.Name = "2P";
            method.Descr = "两点创建直线";
            method.StepNames = new string[3];
            method.StepOptions = new string[3];
            method.StepNames[0] = "SpecifyFirstPoint";
            method.StepOptions[0] = "指定第一个点:";
            method.StepNames[1] = "SpecifyNextPoint";
            method.StepOptions[1] = "指定下一个点或[放弃(U)]:";
            method.StepNames[2] = "GiveUpFirstPoint";
            method.StepOptions[2] = "";
            Methods[0] = method;

            DefaultMethod = method;
        }

        private Vector2d _firstPoint { get; set; }
        private Vector2d _secondPoint { get; set; }

        public LineCreator()
        {
        }
        public override string GetCmdName()
        {
            return CmdName;
        }
        public override CreateMethod[] GetMethods()
        {
            return LineCreator.Methods;
        }
        protected override void ExecuteStep(CreateMethod method, string stepName,string stepArg)
        {
            var tip = GetStepTip(method, stepName);
            if (stepName == "SpecifyFirstPoint")
            {
                CmdInputer.StartInput(tip, (succ, txt) =>
                {
                    if (!succ) { ExecuteEnd(false); return; }
                        
                });
                VPortInputer.StartInputPoint((succ, p) =>
                {
                    if (!succ) { ExecuteEnd(false); return; }
                    _firstPoint = p.Value;
                    var nextStep = GetNextStepName(method, stepName);
                    ExecuteStep(method, nextStep,null);

                });
            }
            else if (stepName == "SpecifyNextPoint")
            {
                VPortInputer.SetAuxLineDraw(AuxLineDraw);
                CmdInputer.StartInput(tip, (succ, txt) =>
                {
                    if (!succ) { ExecuteEnd(false); return; }
                    if (txt.ToUpper() == "U")
                    {
                        VPortInputer.SetAuxLineDraw(null);
                        this.ExecuteStep(method, "SpecifyFirstPoint",null);
                    }
                });

                VPortInputer.StartInputPoint((succ, p) =>
                {
                    if (!succ) { ExecuteEnd(false); return; }
                    _secondPoint = p.Value;
                    CreateLineElement();
                    _firstPoint = _secondPoint;
                    this.ExecuteStep(method, "SpecifyNextPoint",null);

                });

            }
        }
       
        private void CreateLineElement()
        {
            LcDocument.Current.ModelSpace.AddLine(_firstPoint, _secondPoint);
        }
        private void AuxLineDraw(SKCanvas canvas, params object[] args)
        {
            var movePoint = (SKPoint)args[0];
            var start = ElementDrawer.PointWorldToScr(_firstPoint);

            if (start == movePoint) return;

            using (var pen = new SKPaint { Color = SKColors.Silver, IsStroke = true })
            {
                canvas.DrawLine(start, movePoint, pen);
            }
        }

        protected override void EndAction(bool success)
        {
            if(DocumentManager.CurrentRecorder.CurrentActionRecord.Actions.Count>0)
                DocumentManager.CurrentRecorder.EndAction();
            else
                DocumentManager.CurrentRecorder.CancleAction();

        }
    }
}
